#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "rsa.h"

/**
 * @brief Generate prime numbers p and q.
 *
 * Generate the prime numers p and q and store it within the private key.
 *
 * @note For now this is very small primes but we will work on that later.
 * @note Prime liste taken from http://primes.utm.edu/lists/small/10000.txt
 * @param priv Private key where to store the prime numbers
 *
 * @return -1 if an error occured, 0 otherwise
 */
int            generate_primes(t_privateKey *priv)
{
    t_uint64   primes[126] = {58657, 58661, 58679, 58687, 58693, 58699, 58711,
                              58727, 58733, 58741, 58757, 58763, 58771, 58787,
                              58789, 58831, 58889, 58897, 58901, 58907, 58909,
                              58913, 58921, 58937, 58943, 58963, 58967, 58979,
                              58991, 58997, 59009, 59011, 59021, 59023, 59029,
                              59051, 59053, 59063, 59069, 59077, 59083, 59093,
                              59107, 59113, 59119, 59123, 59141, 59149, 59159,
                              59167, 59183, 59197, 59207, 59209, 59219, 59221,
                              59233, 59239, 59243, 59263, 59273, 59281, 59333,
                              59341, 59351, 59357, 59359, 59369, 59377, 59387,
                              59393, 59399, 59407, 59417, 59419, 59441, 59443,
                              59447, 59453, 59467, 59471, 59473, 59497, 59509,
                              59513, 59539, 59557, 59561, 59567, 59581, 59611,
                              59617, 59621, 59627, 59629, 59651, 59659, 59663,
                              59669, 59671, 59693, 59699, 59707, 59723, 59729,
                              59743, 59747, 59753, 59771, 59779, 59791, 59797,
                              59809, 59833, 59863, 59879, 59887, 59921, 59929,
                              59951, 59957, 59971, 59981, 59999, 60013, 60017};

    srand(time(NULL) * getpid() / (time(NULL) % getpid()));
    priv->p = primes[rand() % 126];
    do
        priv->q = primes[rand() % 126];
    while (priv->q == priv->p);
    return (0);
}
